package com.yx.system.controller;

import com.yx.common.annotation.Log;
import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.system.entity.TdBook;
import com.yx.system.entity.TdClassification;
import com.yx.system.service.TdClassificationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 二级分类控制器
 */
@RestController
@RequestMapping("/sys/classification")
public class TdClassificationController2 {

    @Autowired
    private TdClassificationService tdClassificationService;

    /**
     * 按ID查询二级分类
     *
     * @return
     */
    @Log("按ID查询二级分类")
    @PostMapping("findById")
    @RequiresPermissions("sys:classification:findById")
    public R findById(int id) {
        return R.ok().put("classification", tdClassificationService.selectByPrimaryKey(id));
    }

    /**
     * 通过一级分类查询所有二级分类
     *
     * @return
     */
    @Log("通过一级分类查询所有二级分类")
    @PostMapping("findList")
    @RequiresPermissions("sys:classification:findList")
    public R findList(Map<String, Object> map) {
        Query query = new Query(map);
        List<TdClassification> tdClassificationsList = tdClassificationService.queryList(query);
        int total = tdClassificationService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(tdClassificationsList, total, query.getLimit(), query.getPage());
        return R.ok().put("classificationList", pageUtil);
    }

    /**
     * 新增或修改
     *
     * @return
     */
    @Log("新增或修改")
    @PostMapping("saveOrUpdate")
    @RequiresPermissions("sys:classification:saveOrUpdate")
    public R saveOrUpdate(TdClassification tdClassification) {
        if (tdClassification.getId() == 0 || tdClassification.getId() == null) {
            tdClassification.setCreatedate(new Date());
            tdClassification.setIsdelete(false);
            tdClassificationService.insert(tdClassification);
        } else {
            tdClassificationService.updateByPrimaryKey(tdClassification);
        }
        return R.ok();
    }

    /**
     * 删除 (软删除)
     *
     * @return
     */
    @Log("删除 (软删除)")
    @PostMapping("delete")
    @RequiresPermissions("sys:classification:delete")
    public R delete(int id) {
        TdClassification tdClassification = tdClassificationService.selectByPrimaryKey(id);
        tdClassification.setIsdelete(true);
        tdClassificationService.updateByPrimaryKey(tdClassification);
        return R.ok();
    }

}
